package com.usoft;

import akka.dispatch.ExecutionContexts;
import akka.dispatch.Futures;
import akka.dispatch.OnSuccess;
import akka.dispatch.Recover;
import scala.concurrent.ExecutionContextExecutorService;
import scala.concurrent.Future;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 * Created by liyanxin on 2015/1/9.
 */
public class HandlerFutureException {

    public static void main(String args[]) {

        ExecutionContextExecutorService ec = ExecutionContexts.
                fromExecutorService(Executors.newCachedThreadPool());

        Future<Integer> future = Futures.future(new Callable<Integer>() {
            public Integer call() {
                return 1 / 0;
            }
        }, ec).recover(new Recover<Integer>() {
            public Integer recover(Throwable problem) throws Throwable {
                if (problem instanceof ArithmeticException) {
                    problem.printStackTrace();
                    return 0;
                } else
                    throw problem;
            }
        }, ec);
        future.onSuccess(new OnSuccess<Integer>() {
            @Override
            public void onSuccess(Integer result) throws Throwable {
                System.out.println(result);
            }
        }, ec);
    }

}
